package cn.iocoder.yudao.module.system.controller.admin.worksaleinventory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.worksaleinventory.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.worksaleinventory.WorkSaleInventoryDO;
import cn.iocoder.yudao.module.system.service.worksaleinventory.WorkSaleInventoryService;

@Tag(name = "管理后台 - 销售入库")
@RestController
@RequestMapping("/system/work-sale-inventory")
@Validated
public class WorkSaleInventoryController {

    @Resource
    private WorkSaleInventoryService workSaleInventoryService;

    @PostMapping("/create")
    @Operation(summary = "创建销售入库")
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:create')")
    public CommonResult<Integer> createWorkSaleInventory(@Valid @RequestBody WorkSaleInventorySaveReqVO createReqVO) {
        return success(workSaleInventoryService.createWorkSaleInventory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售入库")
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:update')")
    public CommonResult<Boolean> updateWorkSaleInventory(@Valid @RequestBody WorkSaleInventorySaveReqVO updateReqVO) {
        workSaleInventoryService.updateWorkSaleInventory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:delete')")
    public CommonResult<Boolean> deleteWorkSaleInventory(@RequestParam("id") Integer id) {
        workSaleInventoryService.deleteWorkSaleInventory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除销售入库")
                @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:delete')")
    public CommonResult<Boolean> deleteWorkSaleInventoryList(@RequestParam("ids") List<Integer> ids) {
        workSaleInventoryService.deleteWorkSaleInventoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:query')")
    public CommonResult<WorkSaleInventoryRespVO> getWorkSaleInventory(@RequestParam("id") Integer id) {
        WorkSaleInventoryDO workSaleInventory = workSaleInventoryService.getWorkSaleInventory(id);
        return success(BeanUtils.toBean(workSaleInventory, WorkSaleInventoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售入库分页")
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:query')")
    public CommonResult<PageResult<WorkSaleInventoryRespVO>> getWorkSaleInventoryPage(@Valid WorkSaleInventoryPageReqVO pageReqVO) {
        PageResult<WorkSaleInventoryDO> pageResult = workSaleInventoryService.getWorkSaleInventoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkSaleInventoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售入库 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-sale-inventory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkSaleInventoryExcel(@Valid WorkSaleInventoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkSaleInventoryDO> list = workSaleInventoryService.getWorkSaleInventoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售入库.xls", "数据", WorkSaleInventoryRespVO.class,
                        BeanUtils.toBean(list, WorkSaleInventoryRespVO.class));
    }

}